/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.crud.Impl;

import com.startup.musicstore.app.exceptions.MarketInformationPendingException;
import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.AlbumCategory;
import com.startup.musicstore.model.MarketingInformattion;
import com.startup.musicstore.services.crud.AlbumCategoryCrudService;
import com.startup.musicstore.services.crud.MarketingExceptionService;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author given
 */
public class MarketingExceptionServiceImpl implements MarketingExceptionService{
    private static ApplicationContext ctx;
    
    @Override
    public String getRatingsMessage(Long albumCategoryID, Long albumID) {
        ctx = new ClassPathXmlApplicationContext("classpath:com/startup/musicstore/app/conf/applicationContext-*.xml");
        AlbumCategoryCrudService albumCategoryCrudService = (AlbumCategoryCrudService) ctx.getBean("albumCategoryCrudService");
        AlbumCategory albumCategory =  albumCategoryCrudService.findById(albumCategoryID);
        List<Album> albums = albumCategory.getAlbums();
        for(Album a: albums){
            if(albumID.equals(a.getId())){
                MarketingInformattion marketingInformattion = a.getMarketingInformattion();
                String status = "PENDING";
                if(status.equalsIgnoreCase(marketingInformattion.getStatusOfMarketingInformation())){
                    throw new MarketInformationPendingException("Sorry, the marketing status of the Album is pending");
                }
            }
        }
        return "posted";
    }
    
}
